using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using OracleInternal.Common;

namespace Oracle.ManagedDataAccess.Client
{
	public sealed class OracleOnsServerCollection
	{
		private static object s_syncObj = new object();

		internal static Hashtable s_userCopyOfEntries = new Hashtable();

		public int Count
		{
			get
			{
				lock (s_syncObj)
				{
					int result = 0;
					if (ConfigBaseClass.m_ONSMapping != null)
					{
						result = ConfigBaseClass.m_ONSMapping.Count;
					}
					return result;
				}
			}
		}

		public string this[string serverName]
		{
			get
			{
				lock (s_syncObj)
				{
					string result = string.Empty;
					if (s_userCopyOfEntries.ContainsKey(serverName))
					{
						result = s_userCopyOfEntries[serverName]!.ToString();
					}
					return result;
				}
			}
			set
			{
				lock (s_syncObj)
				{
					if (OracleConnection.m_bAtLeastOneConOpen)
					{
						throw new InvalidOperationException(OracleStringResourceManager.GetErrorMesg(ResourceStringConstants.CANNOT_MODIFY_ORACONFIG));
					}
					AddServer(serverName, value);
					if (ProviderConfig.m_bTraceLevelPublic)
					{
						Trace.Write(OracleTraceLevel.Public, OracleTraceTag.Config, OracleTraceClassName.OracleOnsServerCollection, OracleTraceFuncName.Set, $": {serverName} : {value}");
					}
				}
			}
		}

		internal OracleOnsServerCollection()
		{
		}

		public void Add(string serverName, string attributes)
		{
			lock (s_syncObj)
			{
				if (OracleConnection.m_bAtLeastOneConOpen)
				{
					throw new InvalidOperationException(OracleStringResourceManager.GetErrorMesg(ResourceStringConstants.CANNOT_MODIFY_ORACONFIG));
				}
				AddServer(serverName, attributes);
				if (ProviderConfig.m_bTraceLevelPublic)
				{
					Trace.Write(OracleTraceLevel.Public, OracleTraceTag.Config, OracleTraceClassName.OracleOnsServerCollection, OracleTraceFuncName.Add, $": {serverName} : {attributes}");
				}
			}
		}

		public void Remove(string serverName)
		{
			lock (s_syncObj)
			{
				if (OracleConnection.m_bAtLeastOneConOpen)
				{
					throw new InvalidOperationException(OracleStringResourceManager.GetErrorMesg(ResourceStringConstants.CANNOT_MODIFY_ORACONFIG));
				}
				ConfigBaseClass.m_ONSMapping.Remove(serverName);
				if (ProviderConfig.m_bTraceLevelPublic)
				{
					Trace.Write(OracleTraceLevel.Public, OracleTraceTag.Config, OracleTraceClassName.OracleOnsServerCollection, OracleTraceFuncName.Remove, $": {serverName}");
				}
				s_userCopyOfEntries.Remove(serverName);
			}
		}

		private void AddServer(string serverName, string attributes)
		{
			lock (s_syncObj)
			{
				if (string.IsNullOrEmpty(serverName) || string.IsNullOrEmpty(attributes))
				{
					throw new ArgumentException();
				}
				string[] array = attributes.Split(';');
				for (int i = 0; i < array.Length; i++)
				{
					string[] array2 = array[i].Split('=');
					if (2 == array2.Length && string.Compare(array2[0], "nodeList", ignoreCase: true, CultureInfo.InvariantCulture) == 0)
					{
						string value = array2[1];
						if (!string.IsNullOrEmpty(value))
						{
							Dictionary<string, string> dictionary = new Dictionary<string, string>();
							dictionary.Add("nodeList", value);
							ConfigBaseClass.m_ONSMapping.Add(serverName, dictionary);
							s_userCopyOfEntries[serverName] = attributes;
						}
						break;
					}
				}
			}
		}
	}
}
